package com.tupi.project.ui.presenter;

import com.tupi.project.base.BaseRecyclerViewAdapter;
import com.tupi.project.base.RxPresenter;
import com.tupi.project.bean.BaseBean;
import com.tupi.project.bean.PullToRefreshBean;
import com.tupi.project.component.CommonSubscriber;
import com.tupi.project.http.HttpHelper;
import com.tupi.project.ui.contract.PullToRefreshContract;
import com.tupi.project.util.RxUtil;

import java.util.List;

import javax.inject.Inject;

/**
 * PullToRefreshPresenter
 * Created by CPP on 2017/11/27.
 */

public class PullToRefreshPresenter extends RxPresenter<PullToRefreshContract.View> implements
        PullToRefreshContract.Presenter
{
    private HttpHelper mHttpHelper;
    private int mPage;
    
    @Inject
    public PullToRefreshPresenter(HttpHelper httpHelper)
    {
        super();
        mHttpHelper = httpHelper;
    }
    
    @Override
    public void addEvent()
    {
        
    }
    
    private void onRefresh(int page)
    {
        mPage = page;
        addSubscribe(mHttpHelper.reqIndexDiscountRoute(mPage)
                .compose(RxUtil.<BaseBean<PullToRefreshBean, String>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<BaseBean<PullToRefreshBean, String>>(mView)
                {
                    BaseRecyclerViewAdapter adapter = mView.getAdapter();
                    @Override
                    public void onNext(BaseBean<PullToRefreshBean, String> baseBean)
                    {
                        
                        if (baseBean.getSuccessFlg())
                        {
                            List<PullToRefreshBean> list = baseBean.getDetailModelList();
                            adapter.addData(list);
                            adapter.loadMoreComplete();
                            if (baseBean.getCurrPage() == baseBean.getTotalPage())
                            {
                                adapter.loadMoreEnd(mPage == 1);
                            }
                        }
                    }
                    
                    @Override
                    public void onError(Throwable e)
                    {
                        super.onError(e);
                        adapter.loadMoreFail();
                    }
                }));
    }
    
    public void onRefreshBegin()
    {
        
    }
    
    public void onLoadMoreRequested()
    {
        
    }
}
